/**
 * Created by wangxiao on 2015/11/16.
 */

//组织机构

//全局参数
var deptId = null;
Ext.define('MyApp.view.departmentManager', {
    extend: 'Ext.panel.Panel',
    id: 'departmentManager',
    closable: true,
    title: '组织机构',
    layout : 'border',
    departmentTreePanel:null,
    getDepartmentTreePanel:function(){
        var me = this;
        if(Ext.isEmpty(me.departmentTreePanel)){
            me.departmentTreePanel = Ext.create('MyApp.view.DepartmentTreePanel', {
                id: 'MyApp.view.DepartmentManagerTreePanel_ID',
                width : 280,
                region:'west'
            });
            return me.departmentTreePanel;
        }
    },
    employeeGrid:null,
    getEmployeeGrid:function(){
    	var me = this;
    	if(Ext.isEmpty(me.employeeGrid)){
    		me.employeeGrid = Ext.create('MyApp.view.EmployeeGrid',{
    			id:'MyApp.view.EmployeeGrid_ID',
    			region:'center'
    			
    		});
    		return me.employeeGrid;
    	}
    },
    employeeForm: null,
    getEmployeeForm: function(){
    	var me = this;
    	if(Ext.isEmpty(me.employeeForm)){
    		me.employeeForm = Ext.create('MyApp.view.EmployeeForm');
    	}
    	return me.employeeForm;
    },
   
    initComponent: function() {
        var me = this;
        Ext.applyIf(me, {
        	items: [me.getDepartmentTreePanel(),{
            	region:'center',
            	items: [me.getEmployeeForm(), me.getEmployeeGrid()]
            }]
        });
        me.callParent(arguments);
    }
});

/**
 * 定义树面板
 */
Ext.define('MyApp.view.DepartmentTreePanel', {
    extend: 'Ext.tree.Panel',
    height: 480,
    title: "组织机构树",
    frame: true,
    autoScroll: true,
    useArrows: true,
    animate: true,
    rootVisible: true,
    operationMenu: null,
    selectNodeRecord: null,
    initComponent: function() {
        var me = this;
        me.store = Ext.create('MyApp.store.DepartmentTreeStore');
        me.on('itemclick', me.onClickNode, me);
        me.on('itemcontextmenu', me.onCreateMenu, me);
        me.callParent();
    },
    onClickNode: function(node, record, item, index, e, eOpts) {
        var me = this;
        me.selectNodeRecord = record;
        deptId = record.data.id;
        departmentManager = Ext.getCmp('departmentManager'),
    	employeeGrid = departmentManager.employeeGrid,
    	employeeForm = departmentManager.getEmployeeForm();
        me.selectNodeRecord = record;
        employeeForm.loadFormDataFromTree(record);
        //不是叶子结点，展开下级子部门
        if(record.data.leaf != true) {
        	employeeGrid.disabledButtons(true);
            return;
        }else{//点击叶子结点加载右侧部门员工列表
        	employeeGrid.disabledButtons(false);
        	var employeeGrid = Ext.getCmp('MyApp.view.EmployeeGrid_ID');
            employeeGrid.getStore().loadPage(1);
        }
        
    },
    onCreateMenu: function(view, record, item, index, e, eOpts) {
        var me = this,
            operationMenu = me.createCellItem();
        e.preventDefault();
        if(record.raw) {
            if(record.raw.leaf == true) {
                operationMenu.child('#modify').setVisible(true);
                operationMenu.child('#delete').setVisible(true);
            } else {
                operationMenu.child('#modify').setVisible(true);
                operationMenu.child('#delete').setVisible(false);
            }
        } else{
            operationMenu.child('#modify').setVisible(false);
            operationMenu.child('#delete').setVisible(false);
        }
        operationMenu.showAt(e.getXY());
        me.selectNodeRecord = record;
    },
    createCellItem: function() {
        var me = this;
        if(me.operationMenu == null) {
            me.operationMenu = Ext.create('Ext.menu.Menu', {
                items: [{
                    itemId: 'add',
                    text: "增加",
                    handler: me.onAddNode,
                    scope: me
                }, {
                    itemId: 'modify',
                    text: "修改",
                    handler: me.onModifyNode,
                    scope: me
                }, {
                    itemId: 'delete',
                    text: "删除",
                    handler: me.onDeleteNode,
                    scope: me
                }]
            });
        }
        return me.operationMenu;
    },
    getSelectNodeRecord: function() {
        return this.selectNodeRecord;
    },
    getSelectEmpRecord: function() {
        return this.selectEmpRecord;
    },
   
	getDepartmentWin: function(title, actionType) {
        var me = this;
        if (!me.departmentWin) {
            me.departmentWin = Ext.create('MyApp.view.DepartmentWin');
        }
        return me.departmentWin;
    },
    getEmployeeWin: function(title, actionType) {
        var me = this;
        if (!me.employeeWin) {
            me.employeeWin = Ext.create('MyApp.view.EmployeeWin');
        }
        return me.employeeWin;
    },
    showAddNodeWin: function(record) {
        var addNodeWin = this.getDepartmentWin(),
        record = Ext.create('MyApp.model.DepartmentModel',{
    		'parentDeptId': record.get('id')
    	});
        addNodeWin.bindData("add", "增加部门", record);
        addNodeWin.show();
    },
    showAddEmpWin: function(record) {
        var addEmpWin = this.getEmployeeWin(),
    	record = Ext.create('MyApp.model.EmployeeModel',{
    		'deptId': deptId
    	});
        addEmpWin.bindData("add", "增加用户", record);
        addEmpWin.show();
    },
    showModifyNodeWin:function(record){
    	var modifyNodeWin = this.getDepartmentWin();
    	modifyNodeWin.bindData("modify", "修改"+record.get("deptName")+"部门信息", record);
    	modifyNodeWin.show();
    },
    showModifyEmpWin:function(record){
    	var modifyEmpWin = this.getEmployeeWin();
    	modifyEmpWin.bindData("modify", "修改"+record.get("empName")+"基本信息", record);
    	modifyEmpWin.show();
    	
    },
    onAddNode: function(record) {
        var me = this,
            record = me.getSelectNodeRecord();
        me.showAddNodeWin(record);
    },
    onAddEmp:function(record){
    	var me = this,
        record = me.getSelectNodeRecord();
    me.showAddEmpWin(record);
    },
    onModifyNode:function(record){
    	var me = this;
    	var record = me.getSelectNodeRecord();
    	record = Ext.create('MyApp.model.DepartmentModel',record.raw.entity);
    	 me.showModifyNodeWin(record);
    },
    onModifyEmp:function(record){
    	var me = this;
    	if(Ext.isEmpty(record)){
    		record = me.getSelectNodeRecord();
    		record = Ext.create('MyApp.model.EmployeeModel',record.raw.entity);
    	}
    	 me.showModifyEmpWin(record);
    },

    /**
	 * 刷新树的节点
	 */
	onReflushNode : function() {
		var me =this, store = me.getStore(), record = me.getSelectNodeRecord();
		node = store.getNodeById(record.get('id')); 
		store.load({
			node : node
		});
		return;
	},
	onSyncDeparment: function(record) {
        var me = this;
        Ext.Msg.confirm("确认操作", "同步部门数据", function(btn) {
            if(btn == "yes") {
//            	if(Ext.isEmpty(record)){
//            		record = me.getSelectNodeRecord();    		
//            	}
//            	var id = record.get('id');
                Ext.Ajax.request({
                    url: './rs/department/sync',
                    actionMethods: 'POST',
                    jsonData: {
                    },
                    success: function(response) {
                    	var json = Ext.JSON.decode(response.responseText);
                    	Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
                    	var employeeGrid = Ext.getCmp('MyApp.view.DepartmentTreePanel_ID');
                        store=me.getStore();
                        store.load();
                    },
                    exception : function(response) {
                             var json = Ext.decode(response.responseText);
                             Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
                    }
                });
            }
        });
    },
    onSyncEmployee: function(record) {
        var me = this;
        Ext.Msg.confirm("确认操作", "同步职员数据", function(btn) {
            if(btn == "yes") {
            	if(Ext.isEmpty(record)){
            		record = me.getSelectNodeRecord();    		
            	}
//            	var id = record.get('id');
                Ext.Ajax.request({
                    url: './rs/department/syncEmployee',
                    actionMethods: 'POST',
                    jsonData: {
//                    	'id':id
                    },
                    success: function(response) {
                    	var json = Ext.JSON.decode(response.responseText);
                    	Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
                    	var employeeGrid = Ext.getCmp('MyApp.view.EmployeeGrid_ID');
                        store=employeeGrid.getStore();
                        store.load();
                    },
                    exception : function(response) {
                             var json = Ext.decode(response.responseText);
                             Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
                    }
                });
            }
        });
    },
    onDeleteNode: function(record) {//二次确认消息框
        var me = this;
        Ext.Msg.confirm("确认操作", "删除部门信息", function(btn) {
            if(btn == "yes") {
            		record = me.getSelectNodeRecord();    
               var id = record.get('id');
                Ext.Ajax.request({
                    url: './rs/department/deleteDepartment',
                    actionMethods: 'POST',
                    jsonData: {
                        departmentEO:{
                            'id':id
                        }
                    },
                    success: function(response) {
                    	var json = Ext.JSON.decode(response.responseText);
                    	Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
                    	var employeeGrid = Ext.getCmp('MyApp.view.DepartmentTreePanel_ID');
                        store=me.getStore();
                        store.load();
                    },
                    exception : function(response) {
                             var json = Ext.decode(response.responseText);
                             Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
                    }
                });
            }
        });
    },
    onDeleteEmp: function(record) {//二次确认消息框
        var me = this;
        Ext.Msg.confirm("确认操作", "删除员工信息", function(btn) {
            if(btn == "yes") {
            	if(Ext.isEmpty(record)){
            		record = me.getSelectNodeRecord();    
            	}
            	var id = record.get('uid');
                Ext.Ajax.request({
                    url: './rs/department/deleteEmployee',
                    actionMethods: 'POST',
                    jsonData: {
                    	employeeEO:{
                            'uid':id
                        }
                    },
                    success: function(response) {
                    	var json = Ext.JSON.decode(response.responseText);
                    	Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
                    	var employeeGrid = Ext.getCmp('MyApp.view.EmployeeGrid_ID');
                        store=employeeGrid.getStore();
                        store.load();
                    },
                    exception : function(response) {
                             var json = Ext.decode(response.responseText);
                             Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
                    }
                });
            }
        });
    }
});


Ext.define('MyApp.view.DepartmentWin', {
    extend: 'Ext.window.Window',
    width: 380,
    closable: true,
    resizable: true,
    closeAction: 'hide',
    modal: true,
    layout: 'fit',
//    operationUrl: null,
    departmentModel: null,
    departmentForm: null,
    getDepartmentForm: function(){
    	var me = this;
    	if(me.departmentForm==null){
    		me.departmentForm = Ext.create('MyApp.model.DepartmentForm')
    	}
    	return me.departmentForm;
    },
    initComponent: function() {
        var me = this;
        me.items = [me.getDepartmentForm() ],
            me.buttons = [{
                text: "取消",
                handler: function() {
                    me.close();
                }
            }, '->', {
                text: "保存",
                cls:'yellow_button',
                handler: me.onSaveRecord,
                scope: me
            }]
        me.callParent();
    },
    getDepartmentModel: function() {
        if (null == this.departmentModel) {
            this.departmentModel = Ext.create("MyApp.model.DepartmentModel");
        }
        return this.departmentModel;
    },
    loadDepartmentModel: function(record) {
        this.down('form').getForm().loadRecord(record);
        this.departmentModel = record;
    },
    operationUrl: null,
    setOperationUrl: function(actionType) {
    	var me = this;
		if (actionType === 'add') {
			me.operationUrl = './rs/department/addDepartment';
		} else if (actionType === 'modify') {
			me.operationUrl = './rs/department/modifyDepartment';
		}
	},
//    setOperationUrl: function(url) {
//        this.operationUrl = url;
//    },
	 bindData: function(actionType, title, record){
	    	var me = this,
	    	departmentForm = me.getDepartmentForm().getForm();
	    	me.setOperationUrl(actionType);
	    	me.setTitle(title);
	    	departmentForm.loadRecord(record);
	        if (actionType) {
	            if (actionType === "add") {
	            	departmentForm.reset();
	            }
	        }
	    },
    onSaveRecord: function() {
        var me = this,
	        departmentForm = me.getDepartmentForm().getForm(),
	        record = departmentForm.getRecord();
        if (departmentForm.isValid()) {
        	departmentForm.updateRecord(record);
            Ext.Ajax.request({
                url: me.operationUrl,
                jsonData: {
                    'departmentEO': record.data
                },
                success: function(response) {
                    var josn = Ext.JSON.decode(response.responseText);
                    if(Ext.isEmpty(josn.message)) {
                        Ext.getCmp('MyApp.view.DepartmentManagerTreePanel_ID').store.load();
                        Ext.ux.Toast.msg("操作成功!");
                        me.hide();
                    } else {
                        Ext.ux.Toast.msg("操作失败!");
                    }
                },
                exception : function(response) {
                    var json = Ext.decode(response.responseText);
                    Ext.ux.Toast.msg("操作异常!", json.responseStatus.message);
                }
            });
        }
    }
    
});

Ext.define('MyApp.model.DepartmentForm', {
    extend: 'Ext.form.Panel',
    defaultType: 'textfield',
    defaults: {
    	margin: '5 5 5 5',
        labelWidth: 80,
        width: 320
    },
    initComponent: function() {
        var me = this;
        me.items = [{
            fieldLabel: "部门名称",
            name: 'deptName',
            allowBlank: false
        }, {
            fieldLabel: "部门简介",
            name: 'deptDesc',
            allowBlank: false
        },{
        	xtype: 'numberfield',
        	fieldLabel:'显示顺序',
        	name:'displayOrder',
        	display:true,
        	allowBlank:false
        }];
        me.callParent();
    }
});
/**
 * 定义右上功能表单详情
 */
Ext.define('MyApp.view.EmployeeForm', {
	extend : 'Ext.form.Panel',
	title: "部门详情",
	layout:'column',
    defaults: {
    	columnWidth:.5,
    	margin:'5 5 5 5',
    	readOnly: true,
    	xtype: 'textfield'
    },
    dockedItems: [{
        xtype: 'toolbar',
        dock: 'top',
        
        items: [
            { xtype: 'button', text: '同步部门数据', id:'deptparment',handler:function(grid, rowIndex, colIndex, item, e, record, row){
            	var departmentManager = Ext.getCmp('departmentManager'),
            	departmentTreePanel = departmentManager.departmentTreePanel;
//            	departmentTreePanel.onDeleteNode();
            	departmentTreePanel.onSyncDeparment(record);
            }},
            { xtype: 'button', text: '同步职员数据', id:'employee',handler:function(grid, rowIndex, colIndex, item, e, record, row){
            	var departmentManager = Ext.getCmp('departmentManager'),
            	departmentTreePanel = departmentManager.departmentTreePanel;
//            	departmentTreePanel.onDeleteNode();
            	departmentTreePanel.onSyncEmployee(record);
            }},
        ]
    }],
    /**
	 * 从功能树中加载数据到form表单中
	 */
	loadFormDataFromTree : function(record) {
		var me = this,
			form = me.getForm(),
			deptName = form.findField('deptName');
		// 如果父节点为空，那么当前节点就是功能树的根节点，不可以做修改
		if (record.raw.entity == null) {
			return;
		}
		var departmentModel = Ext.create('MyApp.model.DepartmentModel', record.raw.entity);
		me.loadRecord(departmentModel);
	},
	constructor : function(config) {
		var me = this;
    	Ext.applyIf(me, {
	items: [DataDictionary.getDataDictionaryCombo('id',{
		fieldLabel: '部门编号',
		readOnly: true,
		name: 'id'
	}),{
		name: 'deptName',
        fieldLabel: "部门名"
	},{
		name: 'displayOrder',
		fieldLabel: "显示顺序"
	},{
		name: 'deptDesc',
        fieldLabel: "部门简介"
	},{
		name: 'createTime',
		format:'Y-m-d H:i:s',
		xtype: 'datefield',
		fieldLabel: "创建时间"
	},{
		name: 'lastUpdateTime',
		format:'Y-m-d H:i:s',
		xtype: 'datefield',
		fieldLabel: "最后更新时间"
	}]
    	}, config);
		this.callParent([config]); 
	}
});
Ext.define('MyApp.view.EmployeeGrid',{
	extend:'Ext.grid.Panel',
	id: 'employee',
	title:'员工信息管理',
	columnLines: true,
	selectEmpRecord: null,
	disabledButtons: function(flag) {
		this.getAddButton().setDisabled(flag);
	},
	// 新增按钮
	addButton: null,
	getAddButton: function(record) {
		var me = this;
		if(Ext.isEmpty(me.addButton)){
			me.addButton = Ext.create('Ext.Button', {
				text : '新增',
				disabled: true,
				handler : function() {
					var departmentManager = Ext.getCmp('departmentManager'),
					departmentTreePanel = departmentManager.departmentTreePanel;
					departmentTreePanel.onAddEmp(record);
				}
			});
		}
		return this.addButton;
	},
	
	toolbar: null,
	getToolbar: function(){
		var me = this;
		if(Ext.isEmpty(me.toolbar)){
			me.toolbar = Ext.widget('toolbar',{
				dock: 'top',
				items: [me.getAddButton()]
			});
		}
		return me.toolbar;
	},
	pagingToolbar: null,
	getPagingToolbar: function(){
		var me = this;
		if(me.pagingToolbar==null){
			me.pagingToolbar = Ext.widget('pagingtoolbar',{
				store: me.getStore(),
				dock: 'bottom',
				displayInfo: true
			});
		}
		return me.pagingToolbar;
	},
	store: null,
	getStore: function(){
		var me = this;
		if(me.store==null){
			me.store = Ext.create('MyApp.store.EmployeeStore',{
	    		pageSize:20,
	    		listeners:{
	    			beforeload: function(store, operation, eOpts) {
	    				var departmentManager = Ext.getCmp('departmentManager'),
	    					departmentTreePanel = departmentManager.departmentTreePanel;
	    					if(departmentTreePanel!=undefined){
	    						deptId = departmentTreePanel.getSelectNodeRecord().get('id');
	    				
	    					Ext.apply(operation, {
	    						params: {
	    							'id': deptId
	    						}
	    					});
	    					}
					}
	    		}
	    	})
		}
		return me.store;
	},
	columns: [{
		xtype:'rownumberer',
		width:40
	},{
    	xtype:'actioncolumn',
    	align:'center',
    	text:'操作',
    	items:[{
    		icon: './images/chat/handle_48.png',
            tooltip: '修改',
            handler: function(grid, rowIndex, colIndex, item, e, record, row){
            	var departmentManager = Ext.getCmp('departmentManager'),
            	departmentTreePanel = departmentManager.departmentTreePanel;
            	departmentTreePanel.onModifyEmp(record);
            }
    	},'-',{
    		icon: './images/chat/bankcard_delete.png',
            tooltip: '删除',
            handler: function(grid, rowIndex, colIndex, item, e, record, row){
            	var departmentManager = Ext.getCmp('departmentManager'),
            	departmentTreePanel = departmentManager.departmentTreePanel;
//            	departmentTreePanel.onDeleteNode();
            	departmentTreePanel.onDeleteEmp(record);
            }
    	}]
    },{
    	text:'姓名',
    	dataIndex:'empName',
    	flex:1
    },{
    	text:'工号',
    	dataIndex:'jobnumber',
    	flex:1
    }/*,{
    	text:'部门',
    	dataIndex:'deptName',
    	flex:2
    }*/,{
    	text:'职位',
    	dataIndex:'empPosition',
    	flex:1
    },{
    	text:'电话',
    	dataIndex:'mobile',
    	flex:1
    },{
    	text:'邮箱',
    	dataIndex:'email',
    	flex:2
    },{
    	text:'创建时间',
    	format:'Y-m-d H:i:s',
    	xtype: 'datecolumn',
    	dataIndex:'createTime',
    	flex:2
    },{
    	text:'最后更新时间',
    	format:'Y-m-d H:i:s',
    	xtype: 'datecolumn',
    	dataIndex:'lastUpdateTime',
    	flex:2
    }],
    initComponent:function(){
    	var me = this;
    	Ext.applyIf(me, {
    		store: me.getStore(),
        	dockedItems: [me.getToolbar(), me.getPagingToolbar()]
        });
    	me.callParent();
    }
});

Ext.define('MyApp.view.EmployeeWin', {
    extend: 'Ext.window.Window',
    width: 600,
    closable: true,
    resizable: true,
    closeAction: 'hide',
    modal: true,
    layout: 'fit',
    EmployeeForm: null,
    getEmployeeForm: function(){
    	var me = this;
    	if(me.EmployeeForm==null){
    		me.EmployeeForm = Ext.create('MyApp.model.EditEmployeeForm')
    	}
    	return me.EmployeeForm;
    },
    initComponent: function() {
        var me = this;
        me.items = [me.getEmployeeForm() ],
            me.buttons = [{
                text: "取消",
                handler: function() {
                    me.close();
                }
            }, '->', {
                text: "保存",
                cls:'yellow_button',
                handler: me.onSaveRecord,
                scope: me
            }]
        me.callParent();
    },
    operationUrl: null,
    setOperationUrl: function(actionType) {
		var me = this;
		if (actionType === 'add') {
			me.operationUrl = './rs/department/addEmployee';
		} else if (actionType === 'modify') {
			me.operationUrl = './rs/department/updateEmployee';
		}
	},
    bindData: function(actionType, title, record){
    	var me = this,
    		employeeForm = me.getEmployeeForm().getForm();
    	me.setOperationUrl(actionType);
    	me.setTitle(title);
    	employeeForm.loadRecord(record);
        if (actionType) {
            if (actionType === "add") {
            	deptId:record.get("deptId");
            	employeeForm.reset();
            }else if(actionType === "modify"){
            	uid:record.get("uid");
//            employeeForm.reset();
            }
        }
    },
    onSaveRecord: function() {
        var me = this,
	        departmentManager = Ext.getCmp('departmentManager'),
	        departmentTreePanel = departmentManager.getDepartmentTreePanel(),
	        employeeGrid = departmentManager.getEmployeeGrid(),
	        employeeForm = me.getEmployeeForm().getForm(),
	        record = employeeForm.getRecord();
        employeeForm.updateRecord(record);
        if (employeeForm.isValid()) {
            Ext.Ajax.request({
                url: me.operationUrl.toString(),
                jsonData: {
//                    'employeeEO': employeeForm.getFieldValues()
                    'employeeEO': record.data
                },
                success: function(response) {
                    var josn = Ext.JSON.decode(response.responseText);
                    if(Ext.isEmpty(josn.message)) {
//                        Ext.getCmp('MyApp.view.FunctionManagerTreePanel_ID').store.load();
                        Ext.ux.Toast.msg("操作成功!");
                        me.hide();
//                        var departmentManager=Ext.getCmp('departmentManager'),
//                        store = departmentManager.getStore();
                        var employeeGrid = Ext.getCmp('MyApp.view.EmployeeGrid_ID');
                        store=employeeGrid.getStore()
                        store.load();
                        
                    } else {
                        Ext.ux.Toast.msg("操作失败!");
                    }
                },
                exception : function(response) {
                    var json = Ext.decode(response.responseText);
                    Ext.ux.Toast.msg("操作异常!", json.responseStatus.message);
                }
            });
        }
    }
});

Ext.define('MyApp.model.EditEmployeeForm', {
    extend: 'Ext.form.Panel',  
    layout: 'column',
    defaults: {
        margin: '5 5 5 5',
        columnWidth: .5,
        labelWidth: 80,
        xtype: 'textfield'
    },
	/** 根据新增还是修改按钮改变窗体的设置值 */
	setOperatorType: function(state, record) {
		this.operatorType = state;
		var form = this.getForm();
		form.reset();
		
		var EmployeeSeletorModel = Ext.create('Dpap.commonSelector.EmployeeModel');
		if (state == authorization.functions.STATE_ADD) {
			this.setFormFieldsReadOnly(false);
			var functionTree = Ext.getCmp(authorization.functions.CONTENT_ID).getFunctionTree(),
				selectionModel = functionTree.getSelectionModel().getSelection(),
				
			record = Ext.create('Dpap.authorization.EmployeeModel');
			
			if (selectionModel.length > 0) {
				EmployeeSeletorModel.data.EmployeeCode = (selectionModel[0]).data.id;
				EmployeeSeletorModel.data.EmployeeName = (selectionModel[0]).data.text;
			} else {
				EmployeeSeletorModel.data.EmployeeCode = authorization.functions.ROOT_ID;
				EmployeeSeletorModel.data.EmployeeName = authorization.functions.i18n('dpap.authorization.Function.app');
			}
			
			record.data.checkable = authorization.functions.CHECKABLE_YES;
		} else {
			this.setFormFieldsReadOnly(true);
			functionSeletorModel.data.functionCode = record.data.parentCode;
			functionSeletorModel.data.functionName = record.data.parentName;
		}
		record.data.parentCode = functionSeletorModel;
		
		form.loadRecord(record);
	},
	setFormFieldsReadOnly: function(flag) {
		var form = this.getForm();
		form.findField('uid').setReadOnly(flag);
		form.findField('deptName').setReadOnly(flag);
	},
	remoteValidate: function(value, url) {
		var me = this;
		if (Ext.isEmpty(value) || me.getOperatorType() != authorization.functions.STATE_ADD) {
    		return true;
    	}
    	
    	var params = {'functionVo.functionCode': value}; 
    	var resultFlag = 'false';
        Ext.Ajax.request({
            url : authorization.realPath(url),
            params: params,
			async: false,
            success : function(response) {
            	var json = Ext.decode(response.responseText);
            	// 存在
            	if (json.flag) {
            		resultFlag = json.message;
            	} else {
            		resultFlag = true;
            	}
            },
            exception : function(response) {
            	var json = Ext.decode(response.responseText); 
	        	Ext.ux.Toast.msg(
	        			authorization.functions.i18n('dpap.authorization.msginfo'), 
	        			json.message, 'error');
            }
        });
        
        return resultFlag;
	},
    initComponent: function() {
        var me = this;
        me.items = [{
    		name: 'empName',
    		allowBlank: false,
            fieldLabel: "姓名"
    	},{
    		name: 'jobnumber',
    		allowBlank: false,
            fieldLabel: "工号"
    	},{
    		name: 'uid',
    		hidden: true,
            fieldLabel: "uid"
    	}/*,{
    		name: 'deptId',
    		allowBlank: false,
    		hidden: true,
    		value:employeeForm.getRecord().data.id,
            fieldLabel: "部门编号"
    	}*/,{
    		name: 'empPosition',
    		allowBlank: false,
    		fieldLabel: "职位"
    	},{
    		name: 'mobile',
    		allowBlank: false,
    		regex: /(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/,
			regexText: '请输入正确的手机号码(11位)或电话(区号-电话号)！',
    		fieldLabel: "电话"
    	},{
    		name: 'email',
    		allowBlank: false,
    		vtype: 'email',
    		columnWidth: .5,
    		fieldLabel: "邮箱"
    			
    	}];
        me.callParent();
    }
});